package IteratorPattern;

import java.util.Iterator;

public class Test {
    public static void main(String[] args) {
        //BookShelf bookShelf = new BookShelf(4);//
        BookShelf bookShelf = new BookShelf();
        bookShelf.appendBook(new Book("《Java编程思想》"));
        bookShelf.appendBook(new Book("《PHP程序设计》"));
        bookShelf.appendBook(new Book("《C++编程》"));
        bookShelf.appendBook(new Book("《Python程序设计》"));

        //for循环遍历
        System.out.println("for循环遍历");
        for (int i = 0; i < bookShelf.getLength(); i++) {
            Book book = bookShelf.getBookAt(i);
            System.out.println(book.getName());
        }

        //迭代器遍历
        System.out.println("迭代器遍历");
        Iterator iterator = bookShelf.iterator();
        while (iterator.hasNext()) {
            Book book = (Book) iterator.next();//Object转Book类型
            System.out.println(book.getName());
        }

    }
}
